﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Fly.Common.WorkFlow;
using Telerik.Web.UI;
using Telerik.Web.UI.Skins;
using System.Text;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseUpdateCorpInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////获取参数
                if (!string.IsNullOrEmpty(Context.Request["corpguid"]))
                {
                    hdCorpUserGuid.Value = Context.Request["corpguid"];
                }

                if (!string.IsNullOrEmpty(Context.Request["corpname"]))
                {
                    txtCorpName.Value = Context.Request["corpname"].ToString();
                }
                if (!string.IsNullOrEmpty(Context.Request["roleid"]))
                {
                    hdRoleID.Value = Context.Request["roleid"];
                }
                hdOptManGuid.Value = UserInfo.CorpUserGuid;
                hdOptManName.Value = UserInfo.UserName;
                BindAreas();
                switch (Context.Request["action"])
                {
                    case "type":
                        divArea.Style["visibility"] = "hidden";
                        divType.Style["visibility"] = "visible";
                        break;
                    case "zone":
                        divArea.Style["visibility"] = "visible";
                        divType.Style["visibility"] = "hidden";
                        break;
                }
            }

        }

        private void BindAreas()
        {
            BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            List<a_AreaSubInfo> areas = JsonConvert.DeserializeObject<List<a_AreaSubInfo>>(client.GetQu("14"));

            ddlCorpArea.DataValueField = "SortCode";
            ddlCorpArea.DataTextField = "Title";
            ddlCorpArea.DataSource = areas;
            ddlCorpArea.DataBind();
            ddlCorpArea.Items.Insert(0, new ListItem("全部", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            btnSubmit.Style["disabled"] = "disabled";
            string corpUserGuid = hdCorpUserGuid.Value;
            string optManGuid = hdOptManGuid.Value;
            string optManName = hdOptManName.Value;
            ModelService.OperationResult model = new OperationResult();
            using (EnterpriseService.EnterpriseServiceClient client = new EnterpriseService.EnterpriseServiceClient())
            {
                if (divType.Style["visibility"] == "visible")
                {
                    string areaCode = hdAreaCode.Value;
                    int roleID;
                    if (!string.IsNullOrEmpty(hdRoleID.Value.Trim()))
                    {
                        roleID = Convert.ToInt32(hdRoleID.Value);
                    }
                    else
                    {
                        roleID = 0;
                    }
                    int classID = Convert.ToInt32(ddlCorpType.SelectedValue);
                    var result = client.UpdateCorpType(corpUserGuid, roleID, classID, areaCode, optManGuid, optManName);
                    model = JsonConvert.DeserializeObject<ModelService.OperationResult>(result);
                    string msg;
                    if (model.Result)
                    {
                        msg = "<script type='text/javascript'>alert('修改成功！');</script>";
                    }
                    else
                    {
                        msg = "<script type='text/javascript'>alert('修改失败！');</script>";
                    }
                    Literal1.Text = msg;
                }
                if (divArea.Style["visibility"] == "visible")
                {
                    string areaCode = ddlCorpArea.SelectedValue;
                    var result = client.UpdateCorpManageZone(corpUserGuid, areaCode, optManGuid, optManName);
                    model = JsonConvert.DeserializeObject<ModelService.OperationResult>(result);
                    string msg;
                    if (model.Result)
                    {
                        msg = "<script type='text/javascript'>alert('修改成功！');</script>";
                    }
                    else
                    {
                        msg = "<script type='text/javascript'>alert('修改失败！');</script>";
                    }
                    Literal1.Text = msg;
                }
            }
        }
    }
}